package dk.almbrand.microservice.util;

import com.google.gson.Gson;

import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;

import com.google.gson.internal.LinkedTreeMap;

import dk.almbrand.microservice.exception.SystemException;
import dk.almbrand.microservice.model.RequestEvent;

import dk.almbrand.microservice.model.ResponseEvent;

import java.util.Map;

import java.util.TreeMap;

import javax.ejb.Singleton;
import javax.ejb.Startup;

public class GsonUtils<T> {

    public String toJSON(T data) {
        // Use GsonBuilder to enable pretty printing.
        return new GsonBuilder().setPrettyPrinting().create().toJson(data);
    }

    public T fromJSON(String json, Class clazz) throws SystemException {
        try {
            return (T) new Gson().fromJson(json, clazz);
        } catch (JsonParseException e) {
            throw new SystemException("Could not parse json to class " + clazz.getName() + " json: " + json, e);
        }
    }
    
    public static TreeMap toTreeMap(LinkedTreeMap<String, Object> map) {
        TreeMap retMap = new TreeMap();
        retMap.putAll(map);
        return retMap;   
    }
   
}
